package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import android.app.Fragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.thinkcube.edutainment.R;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass. Use the
 * {@link ImageViewFragment#newInstance} factory method to create an instance of
 * this fragment.
 * 
 */
@EFragment(R.layout.fragment_image_view)
public class ImageViewFragment extends Fragment {
	// TODO: Rename parameter arguments, choose names that match
	// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
	private static final String ARG_PARAM1 = "param1";

	private String mParam1;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	@ViewById(R.id.imageView)
	ImageView imageView;
	
	@ViewById(R.id.progressBar)
	ProgressBar progressBar; 

	@AfterViews
	void iniUi() {

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.background)
				.showImageForEmptyUri(R.drawable.icon)
				.showImageOnFail(R.drawable.icon).cacheInMemory(true)
				.cacheOnDisc(true).displayer(new SimpleBitmapDisplayer())
				.build();
		imageLoader = ImageLoader.getInstance();
		System.out.println("image url" + mParam1);

		imageLoader.displayImage(mParam1, imageView, options, new ImageLoadingListener() {
			
			@Override
			public void onLoadingStarted(String arg0, View arg1) {
				// TODO Auto-generated method stub
				progressBar.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
				// TODO Auto-generated method stub
				progressBar.setVisibility(View.GONE);
			}
			
			@Override
			public void onLoadingCancelled(String arg0, View arg1) {
				// TODO Auto-generated method stub
				
			}
		});

	}

	/**
	 * Use this factory method to create a new instance of this fragment using
	 * the provided parameters.
	 * 
	 * @param param1
	 *            Parameter 1.
	 * @param param2
	 *            Parameter 2.
	 * @return A new instance of fragment ImageViewFragment.
	 */
	// TODO: Rename and change types and number of parameters
	public static ImageViewFragment newInstance(String param1) {
		ImageViewFragment fragment = new ImageViewFragment_();
		Bundle args = new Bundle();
		args.putString(ARG_PARAM1, param1);

		fragment.setArguments(args);
		return fragment;
	}

	public ImageViewFragment() {
		// Required empty public constructor
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mParam1 = getArguments().getString(ARG_PARAM1);

		}
	}

}
